library(lattice)
## Warning: package 'lattice' was built under R version 4.5.2
library(vcd)
## Warning: package 'vcd' was built under R version 4.5.2
## Loading required package: grid
library(PASWR2)
## Warning: package 'PASWR2' was built under R version 4.5.2
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.5.2
library(lattice)
library(vcd)
library(PASWR2)

# Load the dataset
data(EPIDURALF)
#View(EPIDURALF)
# 1. Calculate BMI correctly using the columns in the data
# Weight is in kg, Height is in cm (so we divide by 100 to get meters)
EPIDURALF$BMI <- EPIDURALF$kg / (EPIDURALF$cm / 100)^2

# 2. Mosaic plot
mosaic(~doctor + ease, data = EPIDURALF)

# 3. Box-and-whisker plot using the new BMI column

bwplot(doctor ~ BMI, data = EPIDURALF, main="BMI by Doctor")

library(ggplot2)
library(plotly)
## Warning: package 'plotly' was built under R version 4.5.2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
#q4
ggplot(data = iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point()+theme_minimal()

plot_ly(data = iris,x=~Sepal.Length,y=~Sepal.Width,type='scatter',mode='markers')
#q5
data("diamonds")
ggplot(data = diamonds,aes(cut))+geom_bar()+theme_minimal()

cut_count <- as.data.frame(table(diamonds$cut))
colnames(cut_count) <- c("cut","count")
plot_ly(data = cut_count,x=~cut,y=~count,type='bar')
#q7
data("economics")
ggplot(data = economics,aes(y=unemploy,x=date))+geom_point()+theme_bw()

plot_ly(data = economics,x=~date,y=~unemploy,type = 'scatter',mode='markers')

Q8

data(mpg)
ggplot(data = mpg,aes(x=class,y=hwy,fill = class))+geom_boxplot()+theme()

plot_ly(data = mpg,x=~class,y=~hwy,color = ~class,type = 'box')

Q9

data("mtcars")
ggplot(data = mtcars,aes(x=mpg))+geom_histogram(binwidth = 3)+theme()

plot_ly(data=mtcars,x=~mpg,type = 'histogram')

Q10

ggplot(data = mtcars,aes(x=mpg,y=factor(cyl)))+geom_boxplot()+geom_jitter()+theme()

plot_ly(data = mtcars, 
        x = ~mpg, 
        y = ~factor(cyl), 
        type = 'box', 
        boxpoints = 'all',   # This adds the jittered points
        jitter = 0.3,        # Spread of the points
        pointpos = -1.8,     # Position of points relative to box (-2 to 2)
        color = ~factor(cyl)) %>% 
  layout(title = "MPG by Cylinder Count",
         yaxis = list(title = "Cylinders"),
         xaxis = list(title = "Miles per Gallon"))